export default {
  data() {
    return {
      searchItem: {
        searchKey: "",
      },
      showDevice: false,
      styles: {
        height: "calc(100% - 55px)",
        overflow: "auto",
        paddingBottom: "53px",
        position: "static",
      },
      typeList: [
        {
          key: "10",
          title: "老师",
        },
        {
          key: "0",
          title: "学生",
        },
        {
          key: "1",
          title: "住校生",
        },
        {
          key: "2",
          title: "走读生",
        },
        {
          key: "99",
          title: "其他",
        },
      ],
      columns: [
        {
          type: "selection",
          width: 60,
          align: "center",
          fixed: "left",
        },
        {
          title: "设备名称",
          key: "name",
          fixed: "left",
          width: 150,
        },
        {
          title: "设备状态",
          key: "status",
          align: "center",
        },
      ],
      list: [],
      noList: [],
      count: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      datalist: [],

      groupId: "",
      loading: false,
      deviceList: [],
    };
  },
  computed: {
    user() {
      return this.$store.state.user;
    },
  },
  methods: {
    selectMenu(name) {
      this.groupId = name;
      this.getDevice();
    },
    addAction() {
      this.$axios({
        url: `/group/device/group/notin/${this.groupId}`,
        method: "GET",
      }).then((res) => {
        if (res.code == 200) {
          this.noList = res.data;
          this.deviceList = [];
          this.showDevice = true;
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    selectDevices(devices) {
      this.deviceList = devices;
    },

    removeAction() {
      if (this.deviceList.length == 0) {
        this.$Message.error("请先选择设备");
        return;
      }
      this.$Modal.confirm({
        title: "移除设备",
        content: `确定要从权限组中移除选中的设备么？`,
        onOk: () => {
          this.$axios({
            url: `/group/device/group/remove/${this.groupId}`,
            method: "POST",
            data: this.deviceList.map((a) => {
              return a.deviceid;
            }),
          }).then((res) => {
            if (res.code == 200) {
              this.getDevice();
            } else {
              this.$Message.error(res.msg);
            }
          });
        },
      });
    },
    saveInfo() {
      if (this.deviceList.length == 0) {
        this.$Message.error("请先选择设备");
        return;
      }
      this.$axios({
        url: `/group/device/${this.groupId}`,
        method: "PUT",
        data: this.deviceList.map((a) => {
          return a.deviceid;
        }),
      }).then((res) => {
        if (res.code == 200) {
          this.showDevice = false;
          this.getDevice();
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    pageChange(page) {
      this.page = page;
      this.getDevice();
    },
    pageSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getDevice();
    },
    getDevice() {
      this.$axios({
        url: `/group/device/group/in/${this.groupId}`,
        method: "POST",
        data: {
          page: this.page,
          pageSize: this.pageSize,
          searchKey: this.searchItem.searchKey,
        },
      }).then((res) => {
        if (res.code == 200) {
          this.list = res.data.list;
          this.total = res.data.total;
          this.deviceList = [];
        } else {
          this.$Message.error(res.msg);
        }
      });
    },

    getData() {
      this.$axios({
        url: `/group/list`,
        method: "POST",
        data: {
          page: 1,
          pageSize: 10,
          searchKey: "",
        },
      }).then((res) => {
        if (res.code == 200) {
          this.datalist = res.data.list;
          if (this.datalist && this.datalist.length > 0) {
            setTimeout(() => {
              this.groupId = this.datalist[0].id;
              this.getDevice();
            }, 200);
          }
        }
      });
    },
    deleteInfo(index) {
      var that = this;
      var model = this.datalist[index];
      this.$Modal.confirm({
        title: "删除提示",
        content: `确定要删除分组：“${model.title}”么？`,
        onOk: () => {
          this.$axios({
            url: `/group/${model.id}`,
            method: "DELETE",
          }).then((res) => {
            if (res.code == 200) {
              this.$Message.info(res.data);
              that.getData();
            } else {
              this.$Message.error(res.msg);
            }
          });
        },
      });
    },
  },
  created() {
    this.getData();
  },
};
